package net.stogi.dsper.ui.client.module.menu;

import net.stogi.common.gwt.dispatch.client.core.ActionManager;
import net.stogi.common.gwt.frame.client.core.EventBus;
import net.stogi.common.gwt.frame.client.mvp.AbstractPresenter;
import net.stogi.common.mvp.core.behavior.Behavior;
import net.stogi.dsper.ui.client.module.dataobject.CloseDataObjectListEvent;
import net.stogi.dsper.ui.client.module.dataobject.CloseDataObjectPreviewEvent;
import net.stogi.dsper.ui.client.module.dataobject.OpenDataObjectListEvent;
import net.stogi.dsper.ui.client.module.datasource.DataSourceListClosedEvent;
import net.stogi.dsper.ui.client.module.datasource.DataSourceListClosedHandler;
import net.stogi.dsper.ui.client.module.datasource.OpenDataSourceListEvent;
import net.stogi.dsper.ui.client.module.datasource.OpenDataSourceListHandler;
import net.stogi.dsper.ui.client.module.tag.OpenTagEditorEvent;
import net.stogi.dsper.ui.client.module.tag.OpenTagEditorHandler;
import net.stogi.dsper.ui.client.module.tag.TagEditorClosedEvent;
import net.stogi.dsper.ui.client.module.tag.TagEditorClosedHandler;
import net.stogi.dsper.ui.client.module.user.OpenUserProfileListEvent;
import net.stogi.dsper.ui.client.module.user.OpenUserProfileListHandler;
import net.stogi.dsper.ui.client.module.user.UserProfileListClosedEvent;
import net.stogi.dsper.ui.client.module.user.UserProfileListClosedHandler;

class MenuPresenter extends AbstractPresenter {

	private MenuView menuView;

	public MenuPresenter(MenuView menuView, ActionManager actionManager, EventBus eventBus) {
		super(actionManager, eventBus);
		this.menuView = menuView;
		initBehavior();
		initHandlers();
	}

	@Override
	protected void initBehavior() {
		menuView.setOpenDataSourceListBehavior(new Behavior() {

			@Override
			public void behave() {
				eventBus.fireEvent(new OpenDataSourceListEvent());
			}
		});
		menuView.setOpenTagEditorBehavior(new Behavior() {

			@Override
			public void behave() {
				eventBus.fireEvent(new OpenTagEditorEvent());
			}
		});
		menuView.setOpenUserProfileBehavior(new Behavior() {

			@Override
			public void behave() {
				eventBus.fireEvent(new OpenUserProfileListEvent());
			}
		});
	}

	@Override
	protected void initHandlers() {
		eventBus.addHandler(DataSourceListClosedEvent.TYPE, new DataSourceListClosedHandler() {

			@Override
			public void onDataSourceListClosed(DataSourceListClosedEvent event) {
				internalUnblockMenuAndOpenDataObjectList();
			}
		});
		eventBus.addHandler(TagEditorClosedEvent.TYPE, new TagEditorClosedHandler() {

			@Override
			public void onTagEditorClosed(TagEditorClosedEvent event) {
				internalUnblockMenuAndOpenDataObjectList();
			}
		});
		eventBus.addHandler(UserProfileListClosedEvent.TYPE, new UserProfileListClosedHandler() {

			@Override
			public void onUserProfileListClosed(UserProfileListClosedEvent event) {
				internalUnblockMenuAndOpenDataObjectList();
			}
		});
		eventBus.addHandler(OpenDataSourceListEvent.TYPE, new OpenDataSourceListHandler() {

			@Override
			public void onOpenDataSourceList(OpenDataSourceListEvent event) {
				internalBlockMenuAndCloseDataObjectList();
			}
		});
		eventBus.addHandler(OpenTagEditorEvent.TYPE, new OpenTagEditorHandler() {

			@Override
			public void onOpenTagEditor(OpenTagEditorEvent event) {
				internalBlockMenuAndCloseDataObjectList();
			}
		});
		eventBus.addHandler(OpenUserProfileListEvent.TYPE, new OpenUserProfileListHandler() {

			@Override
			public void onOpenUserProfileList(OpenUserProfileListEvent event) {
				internalBlockMenuAndCloseDataObjectList();
			}
		});
	}

	private void internalBlockMenuAndCloseDataObjectList() {
		menuView.block();
		eventBus.fireEvent(new CloseDataObjectListEvent());
		eventBus.fireEvent(new CloseDataObjectPreviewEvent());
	}

	private void internalUnblockMenuAndOpenDataObjectList() {
		menuView.unblock();
		eventBus.fireEvent(new OpenDataObjectListEvent());
	}
}
